package com.imooc.mall.service.impl;

import com.imooc.mall.common.ResultInfo;
import com.imooc.mall.common.ResultInfoUtil;
import com.imooc.mall.dao.ProductMapper;
import com.imooc.mall.dto.ProductDetailDto;
import com.imooc.mall.exception.MallException;
import com.imooc.mall.pojo.Product;
import com.imooc.mall.service.IProductDetailService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import static com.imooc.mall.enums.ProductStatusEnum.DELETE;
import static com.imooc.mall.enums.ProductStatusEnum.OFF_SALE;

@Service
public class ProductDetailServiceImpl implements IProductDetailService {

    @Autowired
    private ProductMapper productMapper;

    @Override
    public ResultInfo<ProductDetailDto> findProductDetail(Integer productId) {
        Product product = productMapper.selectByPrimaryKey(productId);
        if (product==null){
            throw new MallException("无此商品");
        }
        if (product.getStatus().equals(OFF_SALE.getCode()) ||
                product.getStatus().equals(DELETE.getCode())) {
            throw new MallException("商品下架或者删除！");
        }
        ProductDetailDto productDetailDto=new ProductDetailDto();
        BeanUtils.copyProperties(product,productDetailDto);
        return ResultInfoUtil.success(productDetailDto);
    }
}
